package com.qianda.qdminiapp.service;

import com.qianda.qdminiapp.common.constant.CommonConstant;
import com.qianda.qdminiapp.common.rabbitmq.MsgProducer;
import com.qianda.qdminiapp.config.CompanyProperties;
import com.qianda.qdminiapp.dao.order.QqdOrderTowelDao;
import com.qianda.qdminiapp.domain.data.OrderContextData;
import com.qianda.qdminiapp.domain.dto.OrderAndPayDto;
import com.qianda.qdminiapp.domain.model.device.QqdDevice;
import com.qianda.qdminiapp.domain.model.order.Order;
import com.qianda.qdminiapp.common.exception.ExceptionCode;
import com.qianda.qdminiapp.common.exception.SimpleException;
import com.qianda.qdminiapp.domain.model.order.QqdOrderInfo;
import com.qianda.qdminiapp.domain.model.order.QqdOrderTowel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Time;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 统一支付
 * @ClassName PaymentContext
 * @Author cw
 * @Date 2019/8/7 13:36
 * @Version 1.0
 */
@Service
@EnableConfigurationProperties({ CompanyProperties.class })
@Transactional
public class PaymentContext {

    @Autowired
    private QqdOrderTowelDao qqdOrderTowelDao;

    @Autowired
    private QqdOrderTowelService qqdOrderTowelService;

    @Autowired
    private MsgProducer msgProducer;

    @Autowired
    private CompanyProperties companyProperties;

    /*
     *  统一支付请求
     * @Description
     * @Author cw
     * @Date 13:38 2019/8/7
     * @Param []
     **/
    @Transactional
    public Map<String, Object> appPay(OrderAndPayDto orderAndPayDto){
        //获取支付策略
        OrderAndPaymentStrategy orderAndPaymentStrategy = PaymentSelector.getPaymentSelector(orderAndPayDto.getPayType());

        //公共检查部分
        QqdDevice qqdDevice = orderAndPaymentStrategy.CommonCheck(orderAndPayDto.getDeviceId(),orderAndPayDto.getImei(), orderAndPayDto.getUserId(),companyProperties);

        //针对不同的支付做相应的检查
        orderAndPaymentStrategy.checkStatus(orderAndPayDto);

        // 设备自检
        orderAndPaymentStrategy.deviceStatusCheck(orderAndPayDto,qqdDevice);

        //创建订单
        QqdOrderInfo order = null;
        OrderContextData contextData = null;
        try {
            contextData = orderAndPaymentStrategy.createOrder(orderAndPayDto);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SimpleException(ExceptionCode.EX_ORDER_CREATE_ERROR);//订单创建异常
        }

        //发起支付
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> object = orderAndPaymentStrategy.appPay(contextData.getQqdOrderInfo());
        if(object != null){
            result = object;
        }
        result.put("order",contextData.getQqdOrderTowel());
        if(orderAndPayDto.getPayType() == CommonConstant.PayType.PAY_TYPE_BALANCE || orderAndPayDto.getPayType() == CommonConstant.PayType.PAY_TYPE_QQD ){
            msgProducer.sendCommand(contextData.getQqdOrderTowel(), 3);

            //10秒后判断订单是否还是出巾中，如果是就把订单改为出巾异常，且异常类型为出巾超时
            QqdOrderTowel qqdOrderTowel = contextData.getQqdOrderTowel();
            Long orderId = qqdOrderTowel.getId();

            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    QqdOrderTowel qqdOrderTowel = qqdOrderTowelService.getById(orderId);
                    if(qqdOrderTowel.getOrderStatus() == CommonConstant.OrderOutTowelStatus.ORDER_OUT_TOWEL_STATUS_WORKING){
                        qqdOrderTowel.setOrderStatus( CommonConstant.OrderOutTowelStatus.ORDER_OUT_TOWEL_STATUS_ERROR);//出巾异常
                        qqdOrderTowel.setOrderErrorType(CommonConstant.OrderErrorType.ORDER_ERROR_TYPE_13);// 异常原因: 出巾超时
                        qqdOrderTowelDao.updateById(qqdOrderTowel);

                        //发起退款
                        orderAndPaymentStrategy.refundPay(qqdOrderTowel);
                    }
                }
            },18000);

        }



        return result;
    }

}
